﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Drawing.Printing;
using System.Windows;
using System.Windows.Controls;
using Logistics.Configuration;
using Logistics.DataManage;
using System.ComponentModel;

namespace Logistics.Win
{
    /// <summary>
    /// Interaction logic for PanelBaseSetting.xaml
    /// </summary>
    public partial class PanelBaseSetting : UserControl
    {
        protected BaseSetting SettingModel { get; set; }

        private String SettingFilePath = GlobalObjects.ConfigFileLocation;

        public PanelBaseSetting()
        {
            this.SettingModel = new BaseSetting();
            InitializeComponent();
        }
        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            IniteSetting();
        }

        /// <summary>
        /// 初始化
        /// </summary>
        private void IniteSetting()
        {
            IDictionary<String, String> Sites = SitesDAO.GetSites();
            this.SettingModel.Sites = Sites;
            this.SettingModel.SiteId = GlobalObjects.GSetting.MachineSiteId;
            if (String.IsNullOrWhiteSpace(this.SettingModel.SiteId) && this.SettingModel.Sites.Count > 0)
            {
                this.SettingModel.SiteId = this.SettingModel.Sites.Keys.First();
            }

            //
            LoadOutlets(this.SettingModel.SiteId);
            this.SettingModel.OutletId = GlobalObjects.GSetting.MachineOutletId;
            if (String.IsNullOrWhiteSpace(this.SettingModel.OutletId) && this.SettingModel.Outlets.Count > 0)
            {
                this.SettingModel.OutletId = this.SettingModel.Outlets.Keys.First();
            }

            int num = 1;
            String numStr = GlobalObjects.GSetting.MachineID;
            if (!String.IsNullOrWhiteSpace(numStr))
            {
                Int32.TryParse(numStr.Trim(), out num);
            }
            num = num > 0 ? num : 1;
            this.SettingModel.NumID = num;
            
            this.DataContext = SettingModel;
            this.SettingModel.PropertyChanged += new PropertyChangedEventHandler(SettingModel_PropertyChanged);
        }
        
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SettingModel_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "SiteId")
            {
                LoadOutlets(this.SettingModel.SiteId);
            }
        }
        private void LoadOutlets(String siteId)
        {
            if (!String.IsNullOrWhiteSpace(this.SettingModel.SiteId))
            {
                IDictionary<String, String> Outles = OutletsDAO.GetOutletsNamesBySiteId(this.SettingModel.SiteId);
                this.SettingModel.Outlets = Outles;
                if(Outles!=null && Outles.Count>0)
                    this.SettingModel.OutletId = Outles.Keys.First();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnSave_Click(object sender, RoutedEventArgs e)
        {
            String siteId = this.SettingModel.SiteId;
            if (String.IsNullOrWhiteSpace(siteId))
            {
                MessageBox.Show("站点不能为空");
                return;
            }
            String siteName = this.SettingModel.Sites[siteId];

            String outletId = this.SettingModel.OutletId;
            if (String.IsNullOrWhiteSpace(outletId))
            {
                MessageBox.Show("网点不能为空");
                return;
            }
            String outletName = this.SettingModel.Outlets[outletId];

            int num = this.SettingModel.NumID;
            num = num > 0 ? num : 1;
            String numIdString = num > 9 ? num.ToString() : "0" + num;

            GlobalObjects.GSetting.MachineID = numIdString;
            GlobalObjects.GSetting.MachineSiteId = siteId.Trim();
            GlobalObjects.GSetting.MachineSiteName = siteName.Trim();
            GlobalObjects.GSetting.MachineOutletId = outletId.Trim();
            GlobalObjects.GSetting.MachineOutletName = outletName.Trim();

            //更新当前用户的信息
            GlobalObjects.CurrentUser.SiteID = GlobalObjects.GSetting.MachineSiteId;
            GlobalObjects.CurrentUser.SiteName = GlobalObjects.GSetting.MachineSiteName;

            Xceed.Wpf.Toolkit.MessageBox.Show("保存成功");
        }
        
        /// <summary>
        /// 
        /// </summary>
        public class BaseSetting : INotifyPropertyChanged
        {
            public event PropertyChangedEventHandler PropertyChanged;
            public void RaisePropertyChanged(string propertyName)
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
                }
            }

            private IDictionary<String,String> _Sites;
            public IDictionary<String,String> Sites { 
                get{return _Sites;}
                set
                {
                    if (_Sites != value)
                    {
                        _Sites = value;
                        RaisePropertyChanged("Sites");
                    }
                }
            }
            
            private IDictionary<String, String> _Outlets;
            public IDictionary<String, String> Outlets
            {
                get { return _Outlets; }
                set
                {
                    if (_Outlets != value)
                    {
                        _Outlets = value;
                        RaisePropertyChanged("Outlets");
                    }
                }
            }
            
            private String _SiteId;
            public String SiteId
            {
                get { return _SiteId; }
                set
                {
                    if (_SiteId != value)
                    {
                        _SiteId = value;
                        RaisePropertyChanged("SiteId");
                    }
                }
            }

            private String _OutletId;
            public String OutletId
            {
                get { return _OutletId; }
                set
                {
                    if (_OutletId != value)
                    {
                        _OutletId = value;
                        RaisePropertyChanged("OutletId");
                    }
                }
            }

            public int NumID { get; set; }
        }
    }
}
